# coding: utf-8
# from Crypto.Cipher import AES

from django.contrib.auth.models import AbstractBaseUser, User, PermissionsMixin, BaseUserManager
from django.db import models
from django.utils import timezone
from django.conf import settings
from common.models.base import BaseModel
from common.models.position import JPositionCounty, JPositionTown, JPositionVillage


class PostTag(BaseModel):
    title = models.CharField(max_length=20, default='')

    def __unicode__(self):
        return self.title

    class Meta:
        app_label = 'common'
        db_table = 'post_tag'


class Post(BaseModel):
    """ 帖子 """
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'用户')

    title = models.CharField(max_length=200, default='')
    content = models.CharField(max_length=10000, default='')

    type = models.CharField(choices=(('CZ', u'村子'), ('GK', u'公开')), null=True, default='GK', max_length=4)
    zhan_count = models.PositiveIntegerField(u'点赞数量', default=0)

    class Meta:
        app_label = 'common'
        db_table = 'post'

    @property
    def tags(self):
        return PostWithTag.objects.filter(post=self)


class PostWithTag(BaseModel):
    post = models.ForeignKey(Post)
    tag = models.ForeignKey(PostTag)

    class Meta:
        app_label = 'common'
        db_table = 'post_with_tag'


class Comment(BaseModel):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    post = models.ForeignKey(Post)
    parent_comment = models.ForeignKey('self', null=True, blank=True, verbose_name='上级')

    content = models.CharField(max_length=500, default='', null=True)

    class Meta:
        app_label = 'common'
        db_table = 'post_comment'
